package cn.itsource.zcn.org.controller;

import cn.itsource.zcn.org.domain.Department;
import cn.itsource.zcn.org.domain.Employee;
import cn.itsource.zcn.org.domain.Employee;
import cn.itsource.zcn.org.query.DepartmentQuery;
import cn.itsource.zcn.org.service.IEmployeeService;
import cn.itsource.zcn.org.service.IEmployeeService;
import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * (TEmployee)表控制层
 *
 * @author makejava
 * @since 2021-08-10 16:11:14
 */
@RestController
@RequestMapping("/emp")
public class EmployeeController {
    /**
     * 服务对象
     */
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询全部数据
     *
     * @return List<>
     */
    @GetMapping
    public List<Employee> queryAll() {
        return employeeService.queryAll();
    }


    /**
     * 添加或者修改
     * @return AjaxResult
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Employee employee) {
        try{
            if(employee.getId()!=null){
                employeeService.update(employee);
            }else{
                employeeService.save(employee);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
     * 删除数据
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 查询一条数据
     * @return List<>
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id) {
        return employeeService.queryById(id);
    }


    /**
     * 查询高级+分页数据
     * @return QueryResult
     */
    @PostMapping
    public QueryResult queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return employeeService.queryPage(departmentQuery);
    }
    /**
     * 批量删除
     * @return AjaxResult
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody  Long[] ids) {
        try {
            employeeService.deleteBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody Employee employee){
        return employeeService.login(employee);
    }
}
